package com.college_career_system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.college_career_system.dao.EnterpriseMapper;
import com.college_career_system.dao.SchoolMapper;
import com.college_career_system.dao.UserMapper;
import com.college_career_system.pojo.dto.UsersLoginDTO;
import com.college_career_system.pojo.po.Enterprise;
import com.college_career_system.pojo.po.School;
import com.college_career_system.pojo.po.User;
import com.college_career_system.service.UsersLoginService;
import com.college_career_system.utils.*;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: Empty
 * @Date: 2022-12-06-19:39
 * @Description:
 */
@Service
public class UsersLoginServiceImpl  implements UsersLoginService {

    private final UserMapper userMapper;
    private final EnterpriseMapper enterpriseMapper;
    private final SchoolMapper schoolMapper;

    public UsersLoginServiceImpl(UserMapper userMapper, EnterpriseMapper enterpriseMapper, SchoolMapper schoolMapper) {
        this.userMapper = userMapper;
        this.enterpriseMapper = enterpriseMapper;
        this.schoolMapper = schoolMapper;
    }

    @Override
    public JSONObject login(UsersLoginDTO usersLoginDTO) {
        if (StringUtil.hasOneEmptyOrNull(usersLoginDTO.getAccount(),usersLoginDTO.getPassword())){
            return ResponseResult.failure(ResponseCode.STATUS_ERROR);
        }
        if (StringUtil.isEmptyOrNull(usersLoginDTO.getIdentity())) {
            return ResponseResult.failure("身份码为空",ResponseCode.STATUS_ERROR);
        }
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String pwd = RSAUtil.decode(usersLoginDTO.getPassword(),Constant.PRIVATE_KEY);
        switch (usersLoginDTO.getIdentity()){
            case "1":{
                User user =userMapper.selectById(usersLoginDTO.getAccount());
                if (user == null || !bCryptPasswordEncoder.matches(pwd,user.getPwd())) {
                    return ResponseResult.failure("账号或密码错误", ResponseCode.STATUS_NOT_LOGGED_IN);                }
                break;
            }
            case "2":{
                Enterprise enterprise = enterpriseMapper.selectById(usersLoginDTO.getAccount());
                if (enterprise == null || !bCryptPasswordEncoder.matches(pwd,enterprise.getPwd())) {
                    return ResponseResult.failure(ResponseCode.STATUS_ERROR);
                }
                break;
            }
            case "3":{
                School school = schoolMapper.selectById(usersLoginDTO.getAccount());
                if (school == null || !bCryptPasswordEncoder.matches(pwd,school.getPwd())) {
                    return ResponseResult.failure(ResponseCode.STATUS_ERROR);
                }
                break;
            }

        }
        return ResponseResult.success(TokenUtil.getToken(usersLoginDTO));
    }

    @Override
    public JSONObject getCode(HttpSession session, HttpServletResponse response) throws IOException {
        // 使用工具类生成验证码
        String code = VerifyCodeUtils.generateVerifyCode(4);
        // 保存到session中
        session.setAttribute(Constant.CODE, code);
        // 设置响应类型
        response.setContentType("image/png");
        // 根据随机数生成图片，通过response响应图片
        // 将图片转换成base64格式
        // 字节数组输出流在内存中创建一个字节数组缓冲区，所有发送到输出流的数据保存在该字节数组缓冲区中
        ServletOutputStream os = response.getOutputStream();
        //将得到的验证码，使用工具类生成验证码图片，并放入到字节数组缓存区
        VerifyCodeUtils.outputImage(220, 60, os, code);
        return ResponseResult.success();
    }

    @Override
    public JSONObject loginByCode(HttpSession session, HttpServletResponse response, UsersLoginDTO usersLoginDTO) {
        if (StringUtil.hasOneEmptyOrNull(usersLoginDTO.getAccount(),usersLoginDTO.getPassword())){
            return ResponseResult.failure("账号密码禁止为空",ResponseCode.STATUS_ERROR);
        }
        if (StringUtil.isEmptyOrNull(usersLoginDTO.getIdentity())) {
            return ResponseResult.failure("身份码为空",ResponseCode.STATUS_ERROR);
        }
        if (StringUtil.isEmptyOrNull(usersLoginDTO.getCode())){
            return ResponseResult.failure("验证码禁止为空",ResponseCode.STATUS_ERROR);
        }
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String pwd = RSAUtil.decode(usersLoginDTO.getPassword(),Constant.PRIVATE_KEY);
        switch (usersLoginDTO.getIdentity()){
            case "1":{
                User user =userMapper.selectById(usersLoginDTO.getAccount());
                if (user == null || !bCryptPasswordEncoder.matches(pwd,user.getPwd())) {
                    return ResponseResult.failure("账号或密码错误", ResponseCode.STATUS_NOT_LOGGED_IN);
                }
                if (!usersLoginDTO.getCode().equals((String) session.getAttribute(Constant.CODE))){
                    return ResponseResult.failure("验证码错误",ResponseCode.STATUS_ERROR);
                }
                break;
            }
            case "2":{
                Enterprise enterprise = enterpriseMapper.selectById(usersLoginDTO.getAccount());
                if (enterprise == null || !bCryptPasswordEncoder.matches(pwd,enterprise.getPwd())) {
                    return ResponseResult.failure(ResponseCode.STATUS_ERROR);
                }
                if (!usersLoginDTO.getCode().equals((String) session.getAttribute(Constant.CODE))){
                    return ResponseResult.failure("验证码错误",ResponseCode.STATUS_ERROR);
                }
                break;
            }
            case "3":{
                School school = schoolMapper.selectById(usersLoginDTO.getAccount());
                if (school == null || !bCryptPasswordEncoder.matches(pwd,school.getPwd())) {
                    return ResponseResult.failure(ResponseCode.STATUS_ERROR);
                }
                if (!usersLoginDTO.getCode().equals((String) session.getAttribute(Constant.CODE))){
                    return ResponseResult.failure("验证码错误",ResponseCode.STATUS_ERROR);
                }
                break;
            }

        }
        return ResponseResult.success("登录成功",ResponseCode.STATUS_SUCCESS);
    }
}
